// pages/list/list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pxTorpx: 2, //px转化为rpx的系数
    leftLooks: 660,  //rpx
    topLooks: 550,  //rpx
    widthScreen: null,
    heightScreen: null,
    list: [
      { 'num': '电子产品' },
      { 'num': '图书文娱' },
      { 'num': '生活日用' },
      { 'num': '鞋包服饰' },
      { 'num': '运动户外' },
      { 'num': '跑腿服务' },
      { 'num': '失物招领' },
      { 'num': '寻物启事' }
    ],
    id: 0,
    nowPage: 1,    
    pageLength: 1, 
    items: [],
    selectedType: 0,
    showPageButton: true,
    tabbar: {},
    TopIndex: 0,
    show: false,
  },
  //上一页 并搜集formID
  onClickButton1: function (e) {
    var that = this;
    var page = that.data.nowPage - 1;
    if (page >= 1) {
      that.setData({
        nowPage: page,
      });
      that.loadItemDetail(that.data.selectedType);
    }
     //搜集formID
    var user_id = wx.getStorageSync('user_id');
    var formId = e.detail.formId;
    app.saveFormId(user_id, formId);
  },

  //下一页 并搜集formID
  onClickButton2: function (e) {
    var that = this;
    var page = that.data.nowPage + 1;
    var pagelength = that.data.pageLength;
    var pageLength = that.data.pageLength;
    if (page <= pageLength) {
      that.setData({
        nowPage: page,
      });
      that.loadItemDetail(that.data.selectedType);
    }
    // 收集formID
    var user_id = wx.getStorageSync('user_id');
    var formId = e.detail.formId;
    app.saveFormId(user_id, formId);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setStorageSync('id',that.data.id);
    var pxTorpx = that.data.pxTorpx;
    //获取屏幕高宽
    wx.getSystemInfo({
      success: function (res) {
        //rpx
        that.setData({
          widthScreen: res.windowWidth * (750 / res.windowWidth),
          heightScreen: res.windowHeight * (750 / res.windowWidth),
          pxTorpx: (750 / res.windowWidth)
        });
      }
    });
  },

  onShow: function(){
    var that = this;
    var animation = wx.createAnimation({});
    var is_campus = wx.getStorageSync('is_campus');
    var id = wx.getStorageSync('id');
    that.setData({
      is_campus: is_campus,
      id: id
    })
    var time = 440;
    if (app.globalData.isReturn==true){
      animation.translate(0, 0).scale(1).opacity(1).step({ duration: time })
      that.setData({ queueData: animation.export() })
      app.globalData.isReturn =false;
    }
    that.loadItemDetail(that.data.id);
  },

  //点击分类按钮
  onClickType: function (e) {
    var that = this;
    var type_id = e.currentTarget.dataset.id;
    this.setData({
      id: type_id,
      selectedType: type_id,
      nowPage: 1 , //重新加载第一页
      pageLength: 0
    })
    wx.setStorageSync('id', type_id);
    that.loadItemDetail(type_id);
  },

  // 拉取列表信息 通用方法
  loadItemDetail: function (type_id) {
    var that = this;
    var is_campus = that.data.is_campus;
    wx.request({
      url: app.globalData.targetUrl + 'items/',
      method: "get",
      data: {
        'type_id': type_id,
        'page': that.data.nowPage,
        'page_cnt': 5
      },
      success: function (res) {
        if(res.data.data.length == 0){
          that.setData({
            items: [],
            noData:true,
            nowPage:0,
            pageLength:0
          });
        }else{
        that.setData({
          items: res.data.data,
          showPageButton: res.data.data[0].page_length == 0 ? false : true,
          pageLength:res.data.data[0].page_length
        });
        }
      }
    });
  },

  //跳转物品详情
  onItemTap: function (e) {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '友情提示',
        content: '请先注册',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }
        }
      })
    } else {
      var itemId = e.currentTarget.dataset.itemid;
      wx.navigateTo({
        url: '/pages/detail/detail?itemId=' + itemId,
      })
    }
  },

  //拉下
  dropDown: function () {
    this.setData({
      show: true
    })
  },
  //拉回
  dropUp: function () {
    this.setData({
      show: false
    })
  },

  //小松鼠动画
  ClickFairy: function () {
    var that = this;
    var animation = wx.createAnimation({});
    var time = 440;
    var widthScreen = this.data.widthScreen;
    var heightScreen = this.data.heightScreen;
    var leftLooks = that.data.leftLooks;
    var topLooks = that.data.topLooks;
    var pxTorpx = that.data.pxTorpx;
    animation.translate(((widthScreen / 2 - leftLooks) - 20) / pxTorpx, ((heightScreen / 2 - topLooks) - 20) / pxTorpx).scale(0).opacity(0.4).step({ duration: time })
    this.setData({ queueData: animation.export() })
    this.sleep(time);
    wx.navigateTo({
      url: '/pages/publish/publish' ,
      success: res => {
      /*  animation.translate(0, 0).scale(1).opacity(1).step({ duration: time })
        that.setData({ queueData: animation.export() })*/
      }
    })
  },
  
  imagetouchmove: function (e) {
    var that = this;
    var pxTorpx = that.data.pxTorpx;
    var left = e.touches[0].clientX * pxTorpx - 45;//rpx
    var top = e.touches[0].clientY * pxTorpx - 45;//rpx
    if (left < 0 && left) { left = 0 };
    if (left > that.data.widthScreen - 90) { left = that.data.widthScreen - 90 };
    if (top < 0) { top = 0 };
    if (top > that.data.heightScreen - 190) { top = that.data.heightScreen - 190 };

    that.setData({
      leftLooks: left,
      topLooks: top
    })
  },
  imagetouchend: function (e) {
    var that = this;
    var animation = wx.createAnimation({});
    var widthScreen = this.data.widthScreen;
    var left_before = that.data.leftLooks;
    if (left_before == that.data.widthScreen - 90) return;
    var left = that.data.widthScreen - 90;
    that.setData({
      leftLooks: left
    })
    var time = 300;
    var n1 = left_before / widthScreen; //形变系数
    if (n1 < 0.2) { n1 = 0.2 }
    var n2 = (widthScreen - left_before) / widthScreen * 0.7;
    var pxTorpx = that.data.pxTorpx;
    animation.scale(n1, 1 + n2).translate((90 / pxTorpx) * (1 - n1), 0).step({ duration: time });
    that.setData({ queueData: animation.export() });
    that.sleep(time);
    animation.scale(1, 1).translate(0, 0).step({ duration: time });
    that.setData({ queueData: animation.export() });

  },

  //延时函数
  sleep: function (numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
      now = new Date();
      if (now.getTime() > exitTime)
        return;
    }
  },
 
})